<?php
require './封装sql.php';
$str=[
    "host"=>"127.0.0.1",
    "root"=>"root",
    "pwd"=>"root",
    "dbname"=>"db"
];
$DB=new Db($str);
//var_dump($DB);
//计算总记录数
$sql="select count(*) from products";
// var_dump($DB->select($sql));
$total=$DB->select($sql)[0][0];

// echo($total);
//每页数
$size=10;
//求出总页数
 $totalPage=ceil($total/$size);
//  //循环显示页码
//获取当前页码的数据
$currentPage=$_GET['currentPage']??1;
$currentPage=$currentPage<1?1:$currentPage;
$currentPage=$currentPage>$totalPage?$totalPage:$currentPage;
$start=($currentPage-1)*$size;
$sql="select *from products limit $start,$size";
$data=$DB->select($sql);
// var_dump($DB->select($sql));



?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
    table{
        width: 780px;
        border:1px solid #000;
        border-collapse:collapse;
    }
    td,th{
        border: 1px solid #000;
    }
    </style>
</head>
<body>
    <table>
    <tr>
        <th>编号</th>   
        <th>商品名称</th>
        <th>价格</th>   
        <th>数量</th>   

    </tr>
    <?php foreach($data as $item):?>
    <tr>
    <td><?=$item[0]?></td>
    <td><?=$item[1]?></td>
    <td><?=$item[2]?></td>
    <td><?=$item[3]?></td>

    </tr>
    <?php endforeach ;?>
    </table>

     <a href="./分页.php?currentPage=1">首页</a>
     <a href="./分页.php?currentPage=<?=$currentPage-1?>">上一页</a>
   
<!-- 循环显示页码 -->
  <?php for($i=1;$i<=$totalPage;$i++):?>
    <a href="./分页.php?currentPage=<?=$i?>"><?=$i?></a>
<?php endfor?>
<a href="./分页.php?currentPage=<?=$currentPage+1?>">下一页</a>
<a href="./分页.php?currentPage=<?=$totalPage?>">尾页</a>


</body>
</html>
